<?php

class Product extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Product':
	 * @var integer $id
	 * @var integer $fk_business_id
	 * @var string $name
	 * @var string $type
	 * @var string $standards
	 * @var string $material
	 * @var string $logo
	 * @var string $pic1
	 * @var string $pic2
	 * @var string $pic3
	 * @var string $pic4
	 * @var string $pic5
	 * @var integer $recommend
	 * @var integer $state
	 * @var string $create_time
	 */
	public $logo1;
	public $series;
	public $province_name;
	public $city_name;
	public $area_name;
	public $business_name;
	public $business_brand;
	public $business_tel;
	public $business_linkman;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name,series_name','length','max'=>20),
			array('type','length','max'=>50),
			array('standards','length','max'=>50),
			array('material','length','max'=>50),
			array('logo','length','max'=>100),
			array('pic1','length','max'=>100),
			array('pic2','length','max'=>100),
			array('pic3','length','max'=>100),
			array('pic4','length','max'=>100),
			array('pic5','length','max'=>100),
			array('series_name,name,type,standards,material,introduction,logo,logo1', 'required'),
		);
	}

	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->fk_business_id=Yii::app()->session['business_id'];
			$this->state=Yii::app()->params['product_verify'];
			$this->recommend=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		$this->update_time=date('Y-m-d G:i:s');
		$this->logo=$this->logo1;
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->pic1='';$this->pic2='';$this->pic3='';$this->pic4='';$this->pic5='';
		$basePath=Yii::app()->basePath.'/../';
 		preg_match_all('/\ssrc=\"([^\"]*)\"/iUs',$this->introduction,$match);
 		foreach($match[1] as $key=>$val){
 			if($key>2) break;
 			$path=preg_replace('/temp\//','',$val);
 			$pic_name='pic'.($key+1);
 			$this->$pic_name=$path;
 			@mkdir(dirname($basePath.$path), 0777,true);
			@fclose(fopen(dirname($basePath.$path).'/index.htm', 'w'));
			$image = Yii::app()->image->load($basePath.$val);
			$image_size=$image->getImageSize();
			if($image_size['width']>600){//如果图片宽度大于600
				$image->resizeByWidth(600);//图片宽度最多600 高度按原有比例
				$image->save();
			}
			@rename($basePath.$val,$basePath.$path);
 		}
 		$this->introduction=preg_replace('/upload\/temp\/product\//','upload/product/',$this->introduction);
 		
 		if($this->logo=='') return;
 		$logo_path=$this->logo;
 		$this->logo=preg_replace('/temp\//','',$this->logo);
 		@mkdir(dirname($basePath.$this->logo), 0777,true);
		@fclose(fopen(dirname($basePath.$this->logo).'/index.htm', 'w'));
		$image = Yii::app()->image->load($basePath.$logo_path);
		$image->resize(170,90);
		$image->save();
		@rename($basePath.$logo_path,$basePath.$this->logo);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_business_id' => '厂商id',
			'series_name' => '系列名称',
			'name' => '产品名称',
			'type' => '型号',
			'standards' => '规格',
			'material' => '材质',
			'introduction' => '产品简介',
			'logo' => '产品标识',
			'pic1' => 'Pic1',
			'pic2' => 'Pic2',
			'pic3' => 'Pic3',
			'pic4' => 'Pic4',
			'pic5' => 'Pic5',
			'recommend' => '推荐',//0 正常 1 推荐
			'state' => '状态',//0正常 1删除 2未审核
			'create_time' => '创建时间',
			'update_time' => '更新时间',
			'business_name' => '厂家名称',
			'business_brand' => '厂家品牌'
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='logo1';
		return $params;
	}
}